<?php


if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Market extends CI_Controller {

    function __construct() {

        parent::__construct();
        $this->load->model('object_model');
		    $this->load->model("market_model");
        $this->load->model('client_model', "client");
        $this->load->model('client/contact', "contact");
		    $this->load->model('object_name_model');
		    $this->load->model('object_name_model');
        $this->load->model('case_name_model');
    		$this->load->model('users_model');
    		$this->load->model('building_name_model');
    		$this->load->model('object_usage_model');
    		$this->load->model('object_state_model');
    		$this->load->model('emphasis_area_model');
    		$this->load->model('address_city_model');
    		$this->load->model('address_county_model');
		
		    $this->load->model('case_name_model');
        $this->load->model('object_state_model');
    		$this->load->model('market_state_model');
    		$this->load->model('building_name_model');
    		$this->load->model('object_usage_model');
    		$this->load->model('object_styles_model');
    		$this->load->model('affiliated_with_model');
    		$this->load->model('environmental_features_model');
		    $this->load->model('management_model');
    		$this->load->model('land_use_zoning_model');
    		$this->load->model('windows_model');
    		$this->load->model('base_is_located_model');
    		$this->load->model('use_status_model');
		
        $this->load->model('building_exterior_model');        
		    $this->load->model('address_city_model');        
        $this->load->model('address_county_model');
        
        $this->load->model('client_model', 'client');
        $this->load->model('client/contact', 'contact');
    		$this->load->model('main_building_model');
    		$this->load->model('look_mode_model');
    		$this->load->model('currency_model');
    		$this->load->model('area_unit_model');
    		$this->load->model('object_features_model');
    		$this->load->model('public_facilities_model');
    		$this->load->model('fire_and_security_model');
    		$this->load->model('mrt_line_model');
    		$this->load->model('address_city_model');
        $this->load->model('parking_type_model');
        $this->load->model('address_county_model');
		    $this->load->model('mrt_model');
        $this->load->model('school_class_model');
        $this->load->model('emphasis_area_model');
        $this->load->model('circle_model');
		    $this->load->model('users_model');
		
		
    }


    

    public function insert() {


        
        $client = $this->input->post("client");
        $contact = $this->input->post("contact");
        //print_r($_POST);
        if ($_POST) {
          $w_ar = array();  
          if(!empty($_POST["object_case_type"])){

            $object_case_types = $_POST["object_case_type"];
            $o_c_t = '';
            $o_c_t_flag = true;

              //物件案別	(中古屋,預售,標售)
              foreach ($object_case_types as $key => $value) {

                  if ($o_c_t_flag) {

                      $o_c_t_flag = false;
                      $o_c_t.=$value;
                  } else {

                      $o_c_t.="," . $value;
                  }
              }
              $w_ar['object_case_type'] = $o_c_t;
            }
            

            foreach ($_POST as $a_name => $val) {
                if (!empty($val) and !is_array($val)) {
                    $w_ar[$a_name] = $val;
                }
            }
            $w_ar['ctime'] = time();
            $w_ar['mtime'] = time();
            
            //print_r($w_ar);

            $ref_client_verify_code = $this->input->post("ref_client_verify_code");
            $client_data;
            if ($ref_client_verify_code == "false") {

                //echo '//echo 新增到client;';

                $verify_code = MD5(date("Ymd his"));
                $this->load->model("users_model");
                $this->load->library('session');
                $c_ss = $this->session->all_userdata();
                $_head_info = $this->users_model->get_user_by_user('劉陳傳');
                //接待人   ,   經紀人
                $clinet_data["front_man"] = $c_ss["users_id"];
                $clinet_data["broker"] = $c_ss["users_id"];
                //主管
                $clinet_data["head"] = $_head_info->row()->users_id;

                // 新增到client
                if ($client["phone_type"] == 1) {
                    //家中電話
                    $clinet_data = array("name" => $client["name"],"email" => $client["email"], "mobile" => $client["mobile"], "home_phone" => $client["phone"], "ref_client_verify_code" => $verify_code, "front_man" => $c_ss["users_id"], "broker" => $c_ss["users_id"], "head" => $_head_info->row()->users_id);
                } elseif ($client["phone_type"] == 2) {
                    //公司電話
                    $clinet_data = array("name" => $client["name"], "email" => $client["email"],"mobile" => $client["mobile"], "company_phone" => $client["phone"], "ref_client_verify_code" => $verify_code, "front_man" => $c_ss["users_id"], "broker" => $c_ss["users_id"], "head" => $_head_info->row()->users_id);
                } else {
                    //不選
                    $clinet_data = array("name" => $client["name"], "email" => $client["email"],"mobile" => $client["mobile"], "ref_client_verify_code" => $verify_code, "front_man" => $c_ss["users_id"], "broker" => $c_ss["users_id"], "head" => $_head_info->row()->users_id);
                }

                $this->client->insert_client($clinet_data);

                // 新增到client
                if ($contact["phone_type"] == 1) {
                    //家中電話
                    $contact_data = array("name" => $contact["name"],"email" => $contact["email"], "mobile" => $contact["mobile"], "home_phone" => $contact["phone"], "ref_client_verify_code" => $verify_code);
                } elseif ($contact["phone_type"] == 2) {
                    //公司電話
                    $contact_data = array("name" => $contact["name"],"email" => $contact["email"], "mobile" => $contact["mobile"], "company_phone" => $contact["phone"], "ref_client_verify_code" => $verify_code);
                } else {
                    //不選
                    $contact_data = array("name" => $contact["name"],"email" => $contact["email"], "mobile" => $contact["mobile"], "ref_client_verify_code" => $verify_code);
                }

                $this->client->add_contact($contact_data);
                $client_info = $this->client->get_client_by_verify_code($verify_code);
                $client_info = $client_info->row();

                $w_ar["client_id"] = $client_info->client_id;
                $w_ar["ref_client_verify_code"] = $client_info->ref_client_verify_code;

                //$this -> object_model -> insert_of_type_object($w_ar);

                $contact_info2 = $this->contact->get_contact_by_name_verify($verify_code, $contact["name"]);
                $w_ar["client_contact_id"] = $contact_info2->row()->client_contact_id;

                $this->market_model->insert_of_type_object($w_ar);

            } else {

                //echo '//如果有顧客';
                //設定顧客id
                $client_info = $this->client->get_client_by_verify_code($ref_client_verify_code);
                $client_info = $client_info->row();
                $w_ar["client_id"] = $client_info->client_id;

                //搜尋～關係人
                $contact_info = $this->contact->get_contact_by_name_verify($ref_client_verify_code, $contact["name"]);

                if ($contact_info->num_rows() > 0) {

                    //如果資料庫有就關聯到object
                    $contact_info = $contact_info->row();
                    $w_ar["client_contact_id"] = $contact_info->client_contact_id;
                } else {

                    //如果沒有在資料庫找到就寫入contact（關係人）
                    //判斷要寫入住家電話還是公司電話
                    if ($contact["phone_type"] == 1) {
                        $contact_data = array("name" => $contact["name"], "email" => $contact["email"],"mobile" => $contact["mobile"], "home_phone" => $contact["phone"], "ref_client_verify_code" => $ref_client_verify_code);
                    } elseif ($contact["phone_type"] == 2) {
                        $contact_data = array("name" => $contact["name"],"email" => $contact["email"], "mobile" => $contact["mobile"], "company_phone" => $contact["phone"], "ref_client_verify_code" => $ref_client_verify_code);
                    } else {
                        //不選
                        $contact_data = array("name" => $contact["name"],"email" => $contact["email"], "mobile" => $contact["mobile"], "ref_client_verify_code" => $ref_client_verify_code);
                    }

                    $this->contact->insert_contact($contact_data);

                    $contact_info2 = $this->contact->get_contact_by_name_verify($ref_client_verify_code, $contact["name"]);

                     $w_ar["client_contact_id"] = $contact_info2->row()->client_contact_id;
                }

                //echo '//設定verify code';
				
                $w_ar["ref_client_verify_code"] = $client_info->ref_client_verify_code;
                $this->market_model->insert_of_type_object($w_ar);
            }

            redirect("/MarketResearch/market/person_query");
        }
    }

    public function data_entry() {
		
        $html = $this->init("市調資料輸入", "jquery,object,object_ready,date,park,school,address,map", "form");
        $this->load->model('case_name_model');

        $html["case_name"] = $this->case_name_model->get_all();
        $this->load->model('object_state_model');

        $html["object_state"] = $this->object_state_model->get_all();
        $this->load->model('market_state_model');
        $html["market_state"] = $this->market_state_model->get_all();
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_all();
        $this->load->model('object_usage_model');
        $html["object_usage"] = $this->object_usage_model->get_all();
        $this->load->model('object_styles_model');
        $html["object_styles"] = $this->object_styles_model->get_all();
        $this->load->model('affiliated_with_model');
        $html["affiliated_with"] = $this->affiliated_with_model->get_all();
        $this->load->model('environmental_features_model');
        $html["environmental_features"] = $this->environmental_features_model->get_all();
        $this->load->model('land_use_zoning_model');
        $html["land_use_zoning"] = $this->land_use_zoning_model->get_all();
        $this->load->model('management_model');
        $html["management"] = $this->management_model->get_all();
        $this->load->model('windows_model');
        $html["windows"] = $this->windows_model->get_all();
        $this->load->model('base_is_located_model');
        $html["base_is_located"] = $this->base_is_located_model->get_all();
        $this->load->model('use_status_model');
        $html["use_status"] = $this->use_status_model->get_all();
        $this->load->model('main_building_model');
        $html["main_building"] = $this->main_building_model->get_all();
        $this->load->model('look_mode_model');
        $html["look_mode"] = $this->look_mode_model->get_all();
        $this->load->model('currency_model');
        $html["currency"] = $this->currency_model->get_all();
        $this->load->model('area_unit_model');
        $html["area_unit"] = $this->area_unit_model->get_all();
        $this->load->model('object_features_model');
        $html["object_features"] = $this->object_features_model->get_all();
        $this->load->model('public_facilities_model');
        $html["public_facilities"] = $this->public_facilities_model->get_all();
        $this->load->model('fire_and_security_model');
        $html["fire_and_security"] = $this->fire_and_security_model->get_all();
        $this->load->model('mrt_line_model');
        $html["mrt_line"] = $this->mrt_line_model->get_all();
        $this->load->model('parking_type_model');
        $html["parking_type"] = $this->parking_type_model->get_all();
        $this->load->model('address_city_model');
        $html["address_city"] = $this->address_city_model->get_all();
        $this->load->model('address_county_model');
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();
        $this->load->model('school_class_model');
        $html["school_class"] = $this->school_class_model->get_all();
        $this->load->model('emphasis_area_model');
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $this->load->model('circle_model');
        $html["circle"] = $this->circle_model->get_all();
        $this->load->model('building_exterior_model');
        $html["building_exterior"] = $this->building_exterior_model->get_all();

        $this->load->library('object_input_lib', 'object_entry');

        $ob_html = $this->object_input_lib->entry_html();

        $this->load->model('users_model');
        $html["users"] = $this->users_model->get_all_users();

        $html["obj"] = $ob_html;

        $this->load->view('market/market_data_entry', $html);
    }

    /*

      public function data_entry_submit() {

      $this -> load -> model('object_model');
      $data['market_survey_no'] = $this -> input -> post('market_survey_no');
      $data['object_no'] = $this -> input -> post('object_no');
      $data['object_state'] = $this -> input -> post('object_state');
      $data['type_rent'] = $this -> input -> post('type_rent');
      $data['type_sale'] = $this -> input -> post('type_sale');
      $data['owner'] = $this -> input -> post('owner');
      $data['owner_mobile'] = $this -> input -> post('owner_mobile');
      $data['owner_phone'] = $this -> input -> post('owner_phone');
      $data['market_research_status'] = $this -> input -> post('market_research_status');
      $data['principal'] = $this -> input -> post('principal');
      $data['principal_mobile'] = $this -> input -> post('principal_mobile');
      $data['principal_phone'] = $this -> input -> post('principal_phone');
      $data['case_name'] = $this -> input -> post('case_name');
      $data['administrative_area'] = $this -> input -> post('administrative_area');
      $data['building_name'] = $this -> input -> post('building_name');
      $data['owner'] = $this -> input -> post('owner');
      $data['object_usage'] = $this -> input -> post('object_usage');
      $data['object_styles'] = $this -> input -> post('object_styles');
      $data['house_building'] = $this -> input -> post('house_building');
      $data['area'] = $this -> input -> post('area');
      $data['build_number'] = $this -> input -> post('build_number');
      $data['sale_total'] = $this -> input -> post('sale_total');
      $data['sale_parking'] = $this -> input -> post('sale_parking');
      $data['sale_housing_prices'] = $this -> input -> post('sale_housing_prices');
      $data['sale_housing_unit_price'] = $this -> input -> post('sale_housing_unit_price');
      $data['sale_parking_price'] = $this -> input -> post('sale_parking_price');
      $data['sale_parking_unit_price'] = $this -> input -> post('sale_parking_unit_price');
      $data['sale_land_unit_price'] = $this -> input -> post('sale_land_unit_price');
      $data['rent_total'] = $this -> input -> post('rent_total');
      $data['rent_tax'] = $this -> input -> post('rent_tax');
      $data['rent_parking'] = $this -> input -> post('rent_parking');
      $data['rent_housing_rent'] = $this -> input -> post('rent_housing_rent');
      $data['rent_parking_space_rentals'] = $this -> input -> post('rent_parking_space_rentals');
      $data['rent_rent_per_ping'] = $this -> input -> post('rent_rent_per_ping');
      $data['rent_monthly_rent_parking_spaces'] = $this -> input -> post('rent_monthly_rent_parking_spaces');
      $data['rent_deposit_month'] = $this -> input -> post('rent_deposit_month');
      $data['rent_deposit_money'] = $this -> input -> post('rent_deposit_money');
      $data['rent_min'] = $this -> input -> post('rent_min');
      $data['rent_max'] = $this -> input -> post('rent_max');
      $data['rent_lease_hike'] = $this -> input -> post('rent_lease_hike');
      $data['rent_rent_free_period_decoration'] = $this -> input -> post('rent_rent_free_period_decoration');
      $data['rent_ancillary'] = $this -> input -> post('rent_ancillary');
      $data['rent_notarization'] = $this -> input -> post('rent_notarization');
      $data['building_area'] = $this -> input -> post('building_area');
      $data['main_building_area'] = $this -> input -> post('main_building_area');
      $data['subsidiary_building_area'] = $this -> input -> post('subsidiary_building_area');
      $data['postulate'] = $this -> input -> post('postulate');
      $data['total_weight_shaped_area'] = $this -> input -> post('total_weight_shaped_area');
      $data['with_parking_number'] = $this -> input -> post('with_parking_number');
      $data['parking_area'] = $this -> input -> post('parking_area');
      $data['parking_area_number'] = $this -> input -> post('parking_area_number');
      $data['use_area'] = $this -> input -> post('use_area');
      $data['postulate_than'] = $this -> input -> post('postulate_than');
      $data['base_is_located1'] = $this -> input -> post('base_is_located1');
      $data['base_is_located2'] = $this -> input -> post('base_is_located2');
      $data['to_no'] = $this -> input -> post('to_no');
      $data['base_area'] = $this -> input -> post('base_area');
      $data['scope_of_rights1'] = $this -> input -> post('scope_of_rights1');
      $data['scope_of_rights2'] = $this -> input -> post('scope_of_rights2');
      $data['stakeholders_land_area'] = $this -> input -> post('stakeholders_land_area');
      $data['present_value'] = $this -> input -> post('present_value');
      $data['total_present_value1'] = $this -> input -> post('total_present_value1');
      $data['total_present_value2'] = $this -> input -> post('total_present_value2');
      $data['land_use_zoning'] = $this -> input -> post('land_use_zoning');
      $data['volume_rate'] = $this -> input -> post('volume_rate');
      $data['building_coverage_ratio'] = $this -> input -> post('building_coverage_ratio');
      $data['monthly_management_fee'] = $this -> input -> post('monthly_management_fee');
      $data['management_fees_per_ping'] = $this -> input -> post('management_fees_per_ping');
      $data['management'] = $this -> input -> post('management');
      $data['indoor_pattern_house'] = $this -> input -> post('indoor_pattern_house');
      $data['indoor_pattern_hall'] = $this -> input -> post('indoor_pattern_hall');
      $data['indoor_pattern_guard'] = $this -> input -> post('indoor_pattern_guard');
      $data['indoor_pattern_suite'] = $this -> input -> post('indoor_pattern_suite');
      $data['windows'] = $this -> input -> post('windows');
      $data['household_door'] = $this -> input -> post('household_door');
      $data['building_entrance'] = $this -> input -> post('building_entrance');
      $data['households'] = $this -> input -> post('households');
      $data['building_households'] = $this -> input -> post('building_households');
      $data['total_towers'] = $this -> input -> post('total_towers');
      $data['total_households'] = $this -> input -> post('total_households');
      $data['floor'] = $this -> input -> post('floor');
      $data['floor_plan1'] = $this -> input -> post('floor_plan1');
      $data['floor_plan2'] = $this -> input -> post('floor_plan2');
      $data['number_of_lifts1'] = $this -> input -> post('number_of_lifts1');
      $data['number_of_lifts2'] = $this -> input -> post('number_of_lifts2');
      $data['facing_the_lane'] = $this -> input -> post('facing_the_lane');
      $data['use_status'] = $this -> input -> post('use_status');
      $data['lease_maturity'] = $this -> input -> post('lease_maturity');
      $data['lessee_by_industry'] = $this -> input -> post('lessee_by_industry');
      $data['floor_height'] = $this -> input -> post('floor_height');
      $data['building_exterior'] = $this -> input -> post('building_exterior');
      $data['main_building'] = $this -> input -> post('main_building');
      $data['building_completion_date'] = $this -> input -> post('building_completion_date');
      $data['year_of_construction'] = $this -> input -> post('year_of_construction');
      $data['annual_rent'] = $this -> input -> post('annual_rent');
      $data['reported_rate'] = $this -> input -> post('reported_rate');
      $data['construction_company'] = $this -> input -> post('construction_company');
      $data['construction_license_no'] = $this -> input -> post('construction_license_no');
      $data['look_mode'] = $this -> input -> post('look_mode');
      $data['key_management'] = $this -> input -> post('key_management');
      $data['location_map1'] = $this -> input -> post('location_map1');
      $data['location_map2'] = $this -> input -> post('location_map2');
      $data['currency'] = $this -> input -> post('currency');
      $data['area_unit'] = $this -> input -> post('area_unit');
      $data['bank_set_total_amount'] = $this -> input -> post('bank_set_total_amount');
      $data['loan_bank'] = $this -> input -> post('loan_bank');
      $data['other_settings_amount'] = $this -> input -> post('');
      $data['bring_own_money'] = $this -> input -> post('bring_own_money');
      $data['estimated_loan'] = $this -> input -> post('estimated_loan');
      $data['environmental_features'] = $this -> input -> post('environmental_features');
      $data['object_features'] = $this -> input -> post('object_features');
      $data['public_facilities'] = $this -> input -> post('public_facilities');
      $data['fire_and_security'] = $this -> input -> post('fire_and_security');
      $data['circle1'] = $this -> input -> post('circle1');
      $data['circle2'] = $this -> input -> post('circle2');
      $data['park1'] = $this -> input -> post('park1');
      $data['park2'] = $this -> input -> post('park2');
      $data['mrt_line1'] = $this -> input -> post('mrt_line1');
      $data['mrt1'] = $this -> input -> post('mrt1');
      $data['mrt_line2'] = $this -> input -> post('mrt_line2');
      $data['mrt2'] = $this -> input -> post('mrt2');
      $data['school_district_primary'] = $this -> input -> post('school_district_primary');
      $data['school_district_high'] = $this -> input -> post('school_district_high');
      $data['school_district_senior'] = $this -> input -> post('school_district_senior');
      $data['spaces_where_floor'] = $this -> input -> post('spaces_where_floor');
      $data['parking_numbers'] = $this -> input -> post('parking_numbers');
      $data['parking_entrance_height'] = $this -> input -> post('parking_entrance_height');
      $data['public_parking_spaces1'] = $this -> input -> post('public_parking_spaces1');
      $data['public_parking_spaces2'] = $this -> input -> post('public_parking_spaces2');
      $data['separate_parking1'] = $this -> input -> post('separate_parking1');
      $data['separate_parking2'] = $this -> input -> post('separate_parking2');
      $data['car_park_management_fee'] = $this -> input -> post('car_park_management_fee');
      $data['total_management_fee_parking'] = $this -> input -> post('total_management_fee_parking');
      $data['parking_type1'] = $this -> input -> post('parking_type1');
      $data['parking_number1'] = $this -> input -> post('parking_number1');
      $data['parking_price1'] = $this -> input -> post('parking_price1');
      $data['parking_type2'] = $this -> input -> post('parking_type2');
      $data['parking_number2'] = $this -> input -> post('parking_number2');
      $data['parking_price2'] = $this -> input -> post('parking_price2');
      $data['use_parking_number'] = $this -> input -> post('use_parking_number');
      $data['ballot'] = $this -> input -> post('ballot');
      $data['internet'] = $this -> input -> post('internet');
      $data['valuation_date'] = $this -> input -> post('valuation_date');
      $data['valuation_date'] = $this -> input -> post('valuation_date');
      $data['appraisal_agencies'] = $this -> input -> post('appraisal_agencies');
      $data['transaction_prices_near'] = $this -> input -> post('transaction_prices_near');
      $data['bcc'] = $this -> input -> post('bcc');
      $data['status'] = $this -> input -> post('status');
      $data['invalid_description'] = $this -> input -> post('invalid_description');
      $data['remark'] = $this -> input -> post('remark');
      $data['front_man'] = $this -> input -> post('front_man');
      $data['head'] = $this -> input -> post('head');
      $data['assigned_date'] = $this -> input -> post('assigned_date');
      $data['broker'] = $this -> input -> post('broker');

      if ($this -> input -> post('object_case_type') != "")
      $data['object_case_type'] = join(",", $this -> input -> post('object_case_type'));
      $analog_pairing_submit = $this -> input -> post('analog_pairing_submit');

      if ($analog_pairing_submit) {

      $this -> load -> model('object_temporary_model');
      $return_result = $this -> object_temporary_model -> add_object($data);

      if ($return_result > 0) {

      //redirect("/pair/object_analog_pairing/" . $return_result);
      } else {
      echo "插入失敗";
      }
      redirect("/main/index");
      } else {
      $return_result = $this -> object_model -> add_object($data);
      //echo $this->db->last_query();die();
      if ($return_result > 0) {
      //echo "object model";
      redirect("/main/index");
      //redirect("/object/data_query_results");
      } else {
      echo "插入失敗";
      }
      redirect("/main/index");
      }

      redirect("/main/index");
      }
     */

    public function resolve($query_condition, $type = "") {
        $keyword = "";
        $where = array();
        $e = explode('%%', $query_condition);
        if (count($e) > 1) {
            $e0 = explode('|', $e[0]);
            $keyword = $e0[1];
            $e1 = explode('---', $e[1]);
            if (count($e1) > 1) {
                foreach ($e1 as $key => $val) {
                    $e2 = explode('|', $val);
                    $where[$e2[0]] = $e2[1];
                }
            } else {
                $e2 = explode('|', $val);
                $where[$e2[0]] = $e2[1];
            }
        } else {
            $e1 = explode('---', $e[0]);
            if (count($e1) > 1) {
                foreach ($e1 as $key => $val) {
                    $e2 = explode('|', $val);
                    $where[$e2[0]] = $e2[1];
                }
            } else {
                if ($e[0] != "") {
                    $e2 = explode('|', $e[0]);
                    $where[$e2[0]] = $e2[1];
                }
            }
        }
        if ($type == "where")
            return $where;
        else
            return $keyword;
    }

    public function data_query() {

        $html = $this->init("物件资料查询", "jquery,object,object_data_query_ready,date,map,address", "form");
        $this->load->model('case_name_model');
        $html["case_name"] = $this->case_name_model->get_all();

        //print_r($html["case_name"]);

        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_all();
        $this->load->model('object_usage_model');
        $html["object_usage"] = $this->object_usage_model->get_all();
        $this->load->model('object_state_model');
        $html["object_state"] = $this->object_state_model->get_all();
        $this->load->model('emphasis_area_model');
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $this->load->model('address_city_model');
        $html["address_city"] = $this->address_city_model->get_all();
        $this->load->model('address_county_model');
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();

        //administrative_area
        $this->load->view('object_data_query', $html);
    }

    public function person_data_query() {
    	
        $html = $this->init("市調－個人-資料查詢", "object,address", "form");
        
        $html["case_name"] = $this->case_name_model->get_all();
        $html["object_names"] = $this->object_name_model->get_all();
        $html["borkers"] = $this->users_model->get_all_users();
        $html["building_name"] = $this->building_name_model->get_all();
        $html["object_usage"] = $this->object_usage_model->get_all();
        $html["object_state"] = $this->object_state_model->get_all();
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $html["address_city"] = $this->address_city_model->get_all();
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();
		
        $this->load->view('market/person_data_query', $html);
    }

    public function company_data_query() {

        $html = $this->init("市調－公司－資料查詢", "object,address", "form");
        $html["case_name"] = $this->case_name_model->get_all();
        $html["object_names"] = $this->object_name_model->get_all();
        $html["borkers"] = $this->users_model->get_all_users();
        $html["building_name"] = $this->building_name_model->get_all();       
        $html["object_usage"] = $this->object_usage_model->get_all();        
        $html["object_state"] = $this->object_state_model->get_all();        
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $html["address_city"] = $this->address_city_model->get_all();        
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();

        
        $this->load->view('market/company_data_query', $html);
    }

    public function data_query_crid() {

        $html = $this->init("资料查询", "jquery,object,object_data_query_ready,date,map,address", "form");
        $this->load->model('case_name_model');
        $html["case_name"] = $this->case_name_model->get_all();
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_all();
        $this->load->model('object_usage_model');
        $html["object_usage"] = $this->object_usage_model->get_all();
        $this->load->model('object_state_model');
        $html["object_state"] = $this->object_state_model->get_all();
        $this->load->model('emphasis_area_model');
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $this->load->model('address_city_model');
        $html["address_city"] = $this->address_city_model->get_all();
        $this->load->model('address_county_model');
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();

        //administrative_area
        $this->load->view('object_data_query_crid', $html);
    }


	public function update_markte_owner_from_client($sequence = null) {
		
		$this->load->library("base_data_lib");
		$cs=$this->base_data_lib->get_all_clients_array();
		$ms=$this->market_model->get_all();
		
		foreach($ms as $m=>$s ){
			//echo $s->woner;
			if( isset($cs[$s->client_id])){
				$data=array('owner'=>$cs[$s->client_id]);
				$this->market_model->update_object($data, $s->market_research_id);
			}
			
		}
		
	}

    //個人查詢
    public function person_data_query_results($sequence = null) {
    	
		//$this->update_markte_owner_from_client();

        $html = $this->init("市調-個人-資料查詢結果", "jquery,object,pagination", "");
		
		$html["case_name"] 		= $this->case_name_model->get_all();
        $html["object_names"] 	= $this->object_name_model->get_all();
        $html["borkers"] 		= $this->users_model->get_all_users();
        $html["building_name"] = $this->building_name_model->get_all();
        $html["object_usage"] = $this->object_usage_model->get_all();
        $html["object_state"] = $this->object_state_model->get_all();
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $html["address_city"] = $this->address_city_model->get_all();
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();
		
					
        $html["post_data"] = ($_POST)?$_POST:'';
		//print_r($html["post_data"]);
        $this->load->view('market/crid/person_object_crid', $html);
    }


	//個人查詢
    public function person_query() {
    	
		

        $html = $this->init("市調-個人-資料查詢結果", "jquery,object,pagination,market", "");
		
		    $html["case_name"] 		= $this->case_name_model->get_all();
        $html["object_names"] 	= $this->object_name_model->get_all();
        $html["borkers"] 		= $this->users_model->get_all_users();
        $html["building_name"] = $this->building_name_model->get_all();
        $html["object_usage"] = $this->object_usage_model->get_all();
        $html["object_state"] = $this->object_state_model->get_all();
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $html["address_city"] = $this->address_city_model->get_all();
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();
		
					
        $html["post_data"] 		= !empty($_POST)?$_POST:'';
    		$html["reload_type"] 		= 'person';
    		$html["search_page"] 	= $this->load->view('market/crid/search_page', $html,true);
		
        $this->load->view('market/crid/market_crid', $html);
    }

    
	//company查詢
    public function company_query() {
    	
		//$this->update_markte_owner_from_client();

        $html = $this->init("市調-公司-資料查詢結果", "jquery,object,pagination", "");
		
		$html["case_name"] 		= $this->case_name_model->get_all();
        $html["object_names"] 	= $this->object_name_model->get_all();
        $html["borkers"] 		= $this->users_model->get_all_users();
        $html["building_name"] = $this->building_name_model->get_all();
        $html["object_usage"] = $this->object_usage_model->get_all();
        $html["object_state"] = $this->object_state_model->get_all();
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        $html["address_city"] = $this->address_city_model->get_all();
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();
		
					
        $html["post_data"] 		= !empty($_POST)?$_POST:'';
		$html["reload_type"] 		= 'company';
		$html["search_page"] 	= $this->load->view('market/crid/search_page', $html,true);
		
        $this->load->view('market/crid/market_crid', $html);
    }
	
	
	
    //公司查詢
    public function company_data_query_results($sequence = null) {

        $html = $this->init("市調-公司-資料查詢结果", "jquery,object,pagination", "");

        $html["post_data"] = $_POST;
        $this->load->view('market/crid/company_object_crid', $html);
    }

    public function data_query_results($sequence = null) {

        
        $html = $this->init("物件资料查询结果", "jquery,object,pagination", "");
        
        $html["post_data"] = $_POST;
        $this->load->view('crid/object_crid', $html);
    }

    public function data_query_results_json($sequence = null) {

        $html = $this->init("物件资料查询结果", "jquery,object,pagination", "table");
        $this->load->model('object_model');

        $keyword = $this->input->post('keywords');

        $where = array();

        if ($this->input->post('owner') != "")
            $where['owner'] = $this->input->post('owner');
        if ($this->input->post('principal') != "")
            $where['principal'] = $this->input->post('principal');

        if ($this->input->post('type_rent') != "") {
            $where['type_rent'] = $this->input->post('type_rent');
        }
        if ($this->input->post('type_sale') != "") {
            $where['type_sale'] = $this->input->post('type_sale');
        }
        if ($this->input->post('type_help_seal') != "") {
            $where['type_help_seal'] = $this->input->post('type_help_seal');
        }

        if ($this->input->post('administrative_area') != "") {
            $where['administrative_area'] = $this->input->post('administrative_area');
        }
        if ($this->input->post('house_building') != "") {
            $where['house_building'] = $this->input->post('house_building');
        }

        if ($this->input->post('house_building') != "")
            $where['house_building'] = $this->input->post('house_building');
        if ($this->input->post('area') != "")
            $where['area'] = $this->input->post('area');
        if ($this->input->post('case_name') != "")
            $where['case_name'] = $this->input->post('case_name');
        if ($this->input->post('building_name') != "")
            $where['building_name'] = $this->input->post('building_name');
        if ($this->input->post('object_usage') != "")
            $where['object_usage'] = $this->input->post('object_usage');
        if ($this->input->post('object_state') != "")
            $where['object_state'] = $this->input->post('object_state');
        if ($this->input->post('building_area1') != "")
            $where['building_area1'] = $this->input->post('building_area1');
        if ($this->input->post('building_area2') != "")
            $where['building_area2'] = $this->input->post('building_area2');
        if ($this->input->post('base_area1') != "")
            $where['base_area1'] = $this->input->post('base_area1');
        if ($this->input->post('base_area2') != "")
            $where['base_area2'] = $this->input->post('base_area2');
        if ($this->input->post('total1') != "")
            $where['total1'] = $this->input->post('total1');
        if ($this->input->post('total2') != "")
            $where['total2'] = $this->input->post('total2');
        if ($this->input->post('latitude1') != "")
            $where['latitude1'] = $this->input->post('latitude1');
        if ($this->input->post('latitude2') != "")
            $where['latitude2'] = $this->input->post('latitude2');
        if ($this->input->post('longitude1') != "")
            $where['longitude1'] = $this->input->post('longitude1');
        if ($this->input->post('longitude2') != "")
            $where['longitude2'] = $this->input->post('longitude2');

        if ($this->input->post('object_case_type') != "") {
            $oj = $this->input->post('object_case_type');

            //echo 'oj';
            $str = '';
            if (!empty($oj)) {
                //echo count($oj);
                foreach ($oj as $oj_n => $oj_val) {

                    $str .= $oj_val;

                    if (count($oj) - 1 > $oj_n) {
                        $str .= ",";
                    }
                }
            }
            //echo '  - - - - '.$str;

            $where['object_case_type'] = $str;
            //$this->input->post('object_case_type');
        }

        if ($this->input->post('year_of_construction1') != "")
            $where['year_of_construction1'] = $this->input->post('year_of_construction1');
        if ($this->input->post('year_of_construction2') != "")
            $where['year_of_construction2'] = $this->input->post('year_of_construction2');
        if ($this->input->post('broker') != "")
            $where['broker'] = $this->input->post('broker');
        if ($this->input->post('entrust_start1') != "")
            $where['entrust_start1'] = $this->input->post('entrust_start1');
        if ($this->input->post('entrust_start2') != "")
            $where['entrust_start2'] = $this->input->post('entrust_start2');
        if ($this->input->post('entrust_end1') != "")
            $where['entrust_end1'] = $this->input->post('entrust_end1');
        if ($this->input->post('entrust_end2') != "")
            $where['entrust_end2'] = $this->input->post('entrust_end2');
        if ($this->input->post('orderby') != "")
            $where['orderby'] = $this->input->post('orderby');
        if ($this->input->post('sort') != "")
            $where['sort'] = $this->input->post('sort');
        if ($this->input->post('bcc') != "")
            $where['bcc'] = $this->input->post('bcc');
        if ($this->input->post('object_no') != "")
            $where['object_no'] = $this->input->post('object_no');
        if ($this->input->post('floor1') != "")
            $where['floor1'] = $this->input->post('floor1');
        if ($this->input->post('floor2') != "")
            $where['floor2'] = $this->input->post('floor2');
        if ($this->input->post('sctime') != "")
            $where['sctime'] = $this->input->post('sctime');
        if ($this->input->post('ectime') != "")
            $where['ectime'] = $this->input->post('ectime');

        $condition = "";
        foreach ($where as $key => $val) {
            if ($condition == "")
                $condition .= $key . "|" . $val;
            else
                $condition .= "---" . $key . "|" . $val;
        }
        if ($keyword != "")
            $html["query_condition"] = "keyword|" . $keyword . "%%" . $condition;
        else
            $html["query_condition"] = $condition;

        /*         * *******對的*********** */
        //$keyword='';
        $this->load->library('pagination');
        $config['base_url'] = '/object/data_query_results';
        $config['total_rows'] = $this->object_model->query_object_count($where, $keyword);
        $config['per_page'] = 15;
        $limit = $config['per_page'] . "," . $sequence;
        $this->pagination->initialize($config);
        $html["victor_page"] = $this->pagination->create_links();
        /*         * *******對的*********** */

        $html["result"] = $this->object_model->query_object($where, $keyword, $limit);
        //echo $this->db->last_query();
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_building_name();
        //print_r($where);
        //$this->object_sf_josn($where);
        //$show_json;

        $page = isset($_POST['page']) ? $_POST['page'] : 1;
        $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
        $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'object_id';
        $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';

        $query = isset($_POST['query']) ? $_POST['query'] : false;
        $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false;

        $total_rows = $this->object_model->countRec("h_object", $where);

        $jsonData = array('page' => $page, 'total' => $total_rows, 'rows' => array());

        $res = $html["result"];
        foreach ($res as $res => $row) {

            $entry = array('object_id' => $row->object_id, 'cell' => array('object_id' => $row->object_id, 'owner' => $row->owner, 'principal' => $row->principal, 'case_name' => $row->case_name, 'building_area' => $row->building_area, 'sale_total' => $row->sale_total, 'with_parking_number' => $row->with_parking_number, 'house_building' => $row->house_building),);

            $jsonData['rows'][] = $entry;
        }

        echo json_encode($jsonData);
    }

    public function data_query_results_crid2($sequence = null) {

        $html = $this->init("物件资料查询结果", "jquery,object,pagination", "table");
        $this->load->model('object_model');

        $keyword = $this->input->post('keywords');

        $where = array();

        if ($this->input->post('owner') != "")
            $where['owner'] = $this->input->post('owner');
        if ($this->input->post('principal') != "")
            $where['principal'] = $this->input->post('principal');

        if ($this->input->post('type_rent') != "") {
            $where['type_rent'] = $this->input->post('type_rent');
        }
        if ($this->input->post('type_sale') != "") {
            $where['type_sale'] = $this->input->post('type_sale');
        }
        if ($this->input->post('type_help_seal') != "") {
            $where['type_help_seal'] = $this->input->post('type_help_seal');
        }

        if ($this->input->post('administrative_area') != "") {
            $where['administrative_area'] = $this->input->post('administrative_area');
        }
        if ($this->input->post('house_building') != "") {
            $where['house_building'] = $this->input->post('house_building');
        }

        if ($this->input->post('house_building') != "")
            $where['house_building'] = $this->input->post('house_building');
        if ($this->input->post('area') != "")
            $where['area'] = $this->input->post('area');
        if ($this->input->post('case_name') != "")
            $where['case_name'] = $this->input->post('case_name');
        if ($this->input->post('building_name') != "")
            $where['building_name'] = $this->input->post('building_name');
        if ($this->input->post('object_usage') != "")
            $where['object_usage'] = $this->input->post('object_usage');
        if ($this->input->post('object_state') != "")
            $where['object_state'] = $this->input->post('object_state');
        if ($this->input->post('building_area1') != "")
            $where['building_area1'] = $this->input->post('building_area1');
        if ($this->input->post('building_area2') != "")
            $where['building_area2'] = $this->input->post('building_area2');
        if ($this->input->post('base_area1') != "")
            $where['base_area1'] = $this->input->post('base_area1');
        if ($this->input->post('base_area2') != "")
            $where['base_area2'] = $this->input->post('base_area2');
        if ($this->input->post('total1') != "")
            $where['total1'] = $this->input->post('total1');
        if ($this->input->post('total2') != "")
            $where['total2'] = $this->input->post('total2');
        if ($this->input->post('latitude1') != "")
            $where['latitude1'] = $this->input->post('latitude1');
        if ($this->input->post('latitude2') != "")
            $where['latitude2'] = $this->input->post('latitude2');
        if ($this->input->post('longitude1') != "")
            $where['longitude1'] = $this->input->post('longitude1');
        if ($this->input->post('longitude2') != "")
            $where['longitude2'] = $this->input->post('longitude2');

        if ($this->input->post('object_case_type') != "") {
            $oj = $this->input->post('object_case_type');

            //echo 'oj';
            $str = '';
            if (!empty($oj)) {
                //echo count($oj);
                foreach ($oj as $oj_n => $oj_val) {

                    $str .= $oj_val;

                    if (count($oj) - 1 > $oj_n) {
                        $str .= ",";
                    }
                }
            }
            //echo '  - - - - '.$str;

            $where['object_case_type'] = $str;
            //$this->input->post('object_case_type');
        }

        if ($this->input->post('year_of_construction1') != "")
            $where['year_of_construction1'] = $this->input->post('year_of_construction1');
        if ($this->input->post('year_of_construction2') != "")
            $where['year_of_construction2'] = $this->input->post('year_of_construction2');
        if ($this->input->post('broker') != "")
            $where['broker'] = $this->input->post('broker');
        if ($this->input->post('entrust_start1') != "")
            $where['entrust_start1'] = $this->input->post('entrust_start1');
        if ($this->input->post('entrust_start2') != "")
            $where['entrust_start2'] = $this->input->post('entrust_start2');
        if ($this->input->post('entrust_end1') != "")
            $where['entrust_end1'] = $this->input->post('entrust_end1');
        if ($this->input->post('entrust_end2') != "")
            $where['entrust_end2'] = $this->input->post('entrust_end2');
        if ($this->input->post('orderby') != "")
            $where['orderby'] = $this->input->post('orderby');
        if ($this->input->post('sort') != "")
            $where['sort'] = $this->input->post('sort');
        if ($this->input->post('bcc') != "")
            $where['bcc'] = $this->input->post('bcc');
        if ($this->input->post('object_no') != "")
            $where['object_no'] = $this->input->post('object_no');
        if ($this->input->post('floor1') != "")
            $where['floor1'] = $this->input->post('floor1');
        if ($this->input->post('floor2') != "")
            $where['floor2'] = $this->input->post('floor2');
        if ($this->input->post('sctime') != "")
            $where['sctime'] = $this->input->post('sctime');
        if ($this->input->post('ectime') != "")
            $where['ectime'] = $this->input->post('ectime');

        $condition = "";
        foreach ($where as $key => $val) {
            if ($condition == "")
                $condition .= $key . "|" . $val;
            else
                $condition .= "---" . $key . "|" . $val;
        }
        if ($keyword != "")
            $html["query_condition"] = "keyword|" . $keyword . "%%" . $condition;
        else
            $html["query_condition"] = $condition;

        /*
          $config['per_page'] = 15;
          $config['total_rows'] = $this->object_model->query_object_count($where, $keyword);
          //echo $this->db->last_query();die();
          $config['url'] = '/object/data_query_results_ajax';
          $config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
          $limit = $config['per_page'] . ",0";
          $html['page'] = $config;
          $html['sequence'] = $sequence;
         */

        /*         * *******對的*********** */
        //$keyword='';
        $this->load->library('pagination');
        $config['base_url'] = '/object/data_query_results';
        $config['total_rows'] = $this->object_model->query_object_count($where, $keyword);
        $config['per_page'] = 15;
        $limit = $config['per_page'] . "," . $sequence;
        $this->pagination->initialize($config);
        $html["victor_page"] = $this->pagination->create_links();
        /*         * *******對的*********** */

        $html["result"] = $this->object_model->query_object($where, $keyword, $limit);
        //echo $this->db->last_query();
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_building_name();
        //print_r($where);
        //$this->object_sf_josn($where);
        //$show_json;

        /*

          if(false){

          $page =         isset($_POST['page']) ? $_POST['page'] : 1;
          $rp =           isset($_POST['rp']) ? $_POST['rp'] : 10;
          $sortname =     isset($_POST['sortname']) ? $_POST['sortname'] : 'object_id';
          $sortorder =    isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';

          $query =        isset($_POST['query']) ? $_POST['query'] : false;
          $qtype =        isset($_POST['qtype']) ? $_POST['qtype'] : false;

          $total_rows=$this->object_model->countRec("h_object" , $where);

          $jsonData = array(  'page'=>$page,
          'total'=>$total_rows,
          'rows'=>array());

          $res=$html["result"];
          foreach($res as $res=> $row ){

          $entry = array(

          'object_id'=>$row->object_id,

          'cell'=>array(

          'object_id'          => $row->object_id,
          'owner'              => $row->owner,
          'principal'          => $row->principal,
          'case_name'          => $row->case_name,
          'building_area'      => $row->building_area,
          'sale_total'         => $row->sale_total,
          'with_parking_number'=> $row->with_parking_number,
          'house_building'     => $row->house_building

          ),
          );

          $jsonData['rows'][] = $entry;
          }

          echo json_encode($jsonData);

          }else{
         */
        $this->load->view('crid/object_crid2', $html);

        //}
    }

    public function data_query_results_crid($sequence = null) {

        $html = $this->init("物件资料查询结果", "jquery,object,pagination", "table");
        $this->load->model('object_model');

        $keyword = $this->input->post('keywords');

        $where = array();

        if ($this->input->post('owner') != "")
            $where['owner'] = $this->input->post('owner');
        if ($this->input->post('principal') != "")
            $where['principal'] = $this->input->post('principal');

        if ($this->input->post('type_rent') != "") {
            $where['type_rent'] = $this->input->post('type_rent');
        }
        if ($this->input->post('type_sale') != "") {
            $where['type_sale'] = $this->input->post('type_sale');
        }
        if ($this->input->post('type_help_seal') != "") {
            $where['type_help_seal'] = $this->input->post('type_help_seal');
        }

        if ($this->input->post('administrative_area') != "") {
            $where['administrative_area'] = $this->input->post('administrative_area');
        }
        if ($this->input->post('house_building') != "") {
            $where['house_building'] = $this->input->post('house_building');
        }

        if ($this->input->post('house_building') != "")
            $where['house_building'] = $this->input->post('house_building');
        if ($this->input->post('area') != "")
            $where['area'] = $this->input->post('area');
        if ($this->input->post('case_name') != "")
            $where['case_name'] = $this->input->post('case_name');
        if ($this->input->post('building_name') != "")
            $where['building_name'] = $this->input->post('building_name');
        if ($this->input->post('object_usage') != "")
            $where['object_usage'] = $this->input->post('object_usage');
        if ($this->input->post('object_state') != "")
            $where['object_state'] = $this->input->post('object_state');
        if ($this->input->post('building_area1') != "")
            $where['building_area1'] = $this->input->post('building_area1');
        if ($this->input->post('building_area2') != "")
            $where['building_area2'] = $this->input->post('building_area2');
        if ($this->input->post('base_area1') != "")
            $where['base_area1'] = $this->input->post('base_area1');
        if ($this->input->post('base_area2') != "")
            $where['base_area2'] = $this->input->post('base_area2');
        if ($this->input->post('total1') != "")
            $where['total1'] = $this->input->post('total1');
        if ($this->input->post('total2') != "")
            $where['total2'] = $this->input->post('total2');
        if ($this->input->post('latitude1') != "")
            $where['latitude1'] = $this->input->post('latitude1');
        if ($this->input->post('latitude2') != "")
            $where['latitude2'] = $this->input->post('latitude2');
        if ($this->input->post('longitude1') != "")
            $where['longitude1'] = $this->input->post('longitude1');
        if ($this->input->post('longitude2') != "")
            $where['longitude2'] = $this->input->post('longitude2');

        if ($this->input->post('object_case_type') != "") {
            $oj = $this->input->post('object_case_type');

            $str = '';
            if (!empty($oj)) {

                foreach ($oj as $oj_n => $oj_val) {

                    $str .= $oj_val;

                    if (count($oj) - 1 > $oj_n) {
                        $str .= ",";
                    }
                }
            }

            $where['object_case_type'] = $str;
            //$this->input->post('object_case_type');
        }

        if ($this->input->post('year_of_construction1') != "")
            $where['year_of_construction1'] = $this->input->post('year_of_construction1');
        if ($this->input->post('year_of_construction2') != "")
            $where['year_of_construction2'] = $this->input->post('year_of_construction2');
        if ($this->input->post('broker') != "")
            $where['broker'] = $this->input->post('broker');
        if ($this->input->post('entrust_start1') != "")
            $where['entrust_start1'] = $this->input->post('entrust_start1');
        if ($this->input->post('entrust_start2') != "")
            $where['entrust_start2'] = $this->input->post('entrust_start2');
        if ($this->input->post('entrust_end1') != "")
            $where['entrust_end1'] = $this->input->post('entrust_end1');
        if ($this->input->post('entrust_end2') != "")
            $where['entrust_end2'] = $this->input->post('entrust_end2');
        if ($this->input->post('orderby') != "")
            $where['orderby'] = $this->input->post('orderby');
        if ($this->input->post('sort') != "")
            $where['sort'] = $this->input->post('sort');
        if ($this->input->post('bcc') != "")
            $where['bcc'] = $this->input->post('bcc');
        if ($this->input->post('object_no') != "")
            $where['object_no'] = $this->input->post('object_no');
        if ($this->input->post('floor1') != "")
            $where['floor1'] = $this->input->post('floor1');
        if ($this->input->post('floor2') != "")
            $where['floor2'] = $this->input->post('floor2');
        if ($this->input->post('sctime') != "")
            $where['sctime'] = $this->input->post('sctime');
        if ($this->input->post('ectime') != "")
            $where['ectime'] = $this->input->post('ectime');

        $condition = "";
        foreach ($where as $key => $val) {
            if ($condition == "")
                $condition .= $key . "|" . $val;
            else
                $condition .= "---" . $key . "|" . $val;
        }
        if ($keyword != "")
            $html["query_condition"] = "keyword|" . $keyword . "%%" . $condition;
        else
            $html["query_condition"] = $condition;

        /*         * *******對的*********** */

        $this->load->library('pagination');
        $config['base_url'] = '/object/data_query_results';
        $config['total_rows'] = $this->object_model->query_object_count($where, $keyword);
        $config['per_page'] = 15;
        $limit = $config['per_page'] . "," . $sequence;
        $this->pagination->initialize($config);
        $html["victor_page"] = $this->pagination->create_links();
        /*         * *******對的*********** */
        $html['where'] = $where;
        $html["result"] = $this->object_model->query_object($where, $keyword, $limit);
        //echo $this->db->last_query();
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_building_name();

        $show_json = true;
        $html["gets"] = $_GET;

        $this->load->view('crid/object_crid', $html);
    }

    //所有顧客
    public function all_client_array() {

        $this->load->model("client_model");
        $all_client = $this->client_model->get_all();
        $client_array = array();
        foreach ($all_client as $client) {
            $client_array[$client->client_id] = $client->name;
        }

        //print_r($client_array);
        return $client_array;
    }

    //所有聯絡人
    public function all_contact_array() {

        $this->load->model("client/contact", 'contact');
        $all_contact = $this->contact->get_all_contact();
        $contact_array = array();

        foreach ($all_contact->result() as $contact) {
            $contact_array[$contact->client_contact_id] = $contact->name;
        }
        //print_r($contact_array);
        return $contact_array;
    }
    /*
    public function object_sf_josn() {

        $page = isset($_POST['page']) ? $_POST['page'] : 1;
        $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
        $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'object_id';
        $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';

        $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false;
        $query = isset($_POST['queryquery']) ? $_POST['queryquery'] : false;

        $v_case_name = isset($_POST['v_case_name']) ? $_POST['v_case_name'] : false;
        $owner = isset($_POST['owner']) ? $_POST['owner'] : false;

        $v_building_name = isset($_POST['v_building_name']) ? $_POST['v_building_name'] : false;

        $house_building = isset($_POST['house_building']) ? $_POST['house_building'] : false;

        $building_area1 = isset($_POST['building_area1']) ? $_POST['building_area1'] : false;
        $building_area2 = isset($_POST['building_area2']) ? $_POST['building_area2'] : false;

        $total1 = isset($_POST['total1']) ? $_POST['total1'] : false;
        $total2 = isset($_POST['total2']) ? $_POST['total2'] : false;

        $object_case_type = isset($_POST['object_case_type']) ? $_POST['object_case_type'] : false;
        $broker = isset($_POST['broker']) ? $_POST['broker'] : false;

        $entrust_start1 = isset($_POST['entrust_start1']) ? $_POST['entrust_start1'] : false;
        $entrust_start2 = isset($_POST['entrust_start2']) ? $_POST['entrust_start2'] : false;
        $object_no = isset($_POST['object_no']) ? $_POST['object_no'] : false;
        $remark = isset($_POST['remark']) ? $_POST['remark'] : false;
        $year_of_construction1 = isset($_POST['year_of_construction1']) ? $_POST['year_of_construction1'] : false;
        $year_of_construction2 = isset($_POST['year_of_construction2']) ? $_POST['year_of_construction2'] : false;

        $latitude1 = isset($_POST['latitude1']) ? $_POST['latitude1'] : false;
        $latitude2 = isset($_POST['latitude2']) ? $_POST['latitude2'] : false;
        $longitude1 = isset($_POST['longitude1']) ? $_POST['longitude1'] : false;
        $longitude2 = isset($_POST['longitude2']) ? $_POST['longitude2'] : false;

        $base_area1 = isset($_POST['base_area1']) ? $_POST['base_area1'] : false;
        $base_area2 = isset($_POST['base_area2']) ? $_POST['base_area2'] : false;

        $area = isset($_POST['area']) ? $_POST['area'] : false;

        $type_rent = isset($_POST['type_rent']) ? $_POST['type_rent'] : false;
        $type_sale = isset($_POST['type_sale']) ? $_POST['type_sale'] : false;
        $type_help_seal = isset($_POST['type_help_seal']) ? $_POST['type_help_seal'] : false;
        //administrative_area
        $administrative_area = isset($_POST['administrative_area']) ? $_POST['administrative_area'] : false;

        $where = '';
        $where_flag = TRUE;

        if ($administrative_area) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `administrative_area` LIKE '%" . $administrative_area . "%' ";
        }

        if ($type_rent) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `type_rent` = '1' ";
        }

        if ($type_sale) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `type_sale` = '1' ";
        }

        if ($type_help_seal) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `type_help_seal` = '1' ";
        }
        if ($object_no) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `object_no` LIKE '%" . $object_no . "%' ";
        }

        if ($area) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `area` LIKE '%" . $area . "%' ";
        }

        if ($base_area1) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `base_area` >= '" . $base_area1 . "' ";
        }

        if ($base_area2) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `base_area` <= '" . $base_area2 . "' ";
        }

        if ($latitude1) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `location_map1` >= '" . $latitude1 . "' ";
        }

        if ($latitude2) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `location_map1` <= '" . $latitude2 . "' ";
        }

        if ($longitude1) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `location_map2` >= '" . $longitude1 . "' ";
        }

        if ($longitude2) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `location_map2` <= '" . $longitude2 . "' ";
        }

        if ($year_of_construction1) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `year_of_construction` >= '" . $year_of_construction1 . "' ";
        }

        if ($year_of_construction2) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `year_of_construction` <= '" . $year_of_construction2 . "' ";
        }

        if ($v_case_name) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `case_name` LIKE '%" . $v_case_name . "%' ";
        }

        if ($broker) {
            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }
            $where .= "  `broker`  >= '" . $broker . "' ";
        }

        if ($total1) {
            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }
            $where .= "  `sale_total`  >= '" . $total1 . "' ";
        }

        if ($total2) {
            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }
            $where .= "  `sale_total` <= '" . $total2 . "' ";
        }

        if ($owner) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `owner` LIKE '%" . $owner . "%' ";
        }

        if ($building_area1) {

            if ($where_flag) {

                $where .= " WHERE ";
                $where_flag = false;
            } else {

                $where .= " AND ";
            }

            $where .= "  `building_area` >= '" . $building_area1 . "' ";
        }

        if ($building_area2) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `building_area` <= '" . $building_area2 . "' ";
        }

        if ($object_case_type) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `object_case_type` LIKE '%" . $object_case_type . "%' ";
        }

        if ($v_building_name) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `building_name` LIKE '%" . $v_building_name . "%' ";
        }

        if ($house_building) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }

            $where .= "  `house_building` LIKE '%" . $house_building . "%' ";
        }

        //經紀人是否可見
        $CI = &get_instance();
        $purview = $CI->login_lib->purview();
        $users_id = $CI->login_lib->users_id();

        $this->load->library('show_test');
        if (!$this->show_test->is_broker_visible_object()) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }

            $where .= "  `broker` = '" . $users_id . "' ";
        }

        $this->load->model('insert_model');
        $sql_data = array('SQL' => $where);
        if (!empty($where)) {
            $this->insert_model->insert_to_sql($sql_data, 'h_sql');
        }

        $this->load->model('importance_of_customers_model');
        $impo = $this->importance_of_customers_model->get_all();
        $impo_s = array();

        foreach ($impo as $key => $value) {
            $impo_s[$value->importance_of_customers_id] = $value->value;
        }

        $this->load->model('object_model');
        $query = $this->object_model->object_for_crid($page, $rp, $sortname, $sortorder, $where);

        header("Content-type: application/json");

        $total_rows = $this->object_model->countRec("h_object", $where);

        $jsonData = array('page' => $page, 'total' => $total_rows, 'rows' => array());

        $html_chbox = '<input type=checkbox onClick=save_client(\'';

        $client_info = $this->all_client_array();
        $contact_info = $this->all_contact_array();
        $users_info = $this->get_all_users();
        $usage_info = $this->get_object_usage();
        foreach ($query->result() as $row) {

            $client_name = '';
            $contact_name = '';
            $broker_name = '';
            $usage_name = '';

            foreach ($usage_info as $kay => $value) {
                if ($kay == $row->object_usage) {
                    $usage_name = $value;
                }
            }

            foreach ($client_info as $kay => $value) {
                if ($kay == $row->client_id) {
                    $client_name = $value;
                }
            }

            foreach ($contact_info as $_k => $_v) {
                if ($_k == $row->client_contact_id) {
                    $contact_name = $_v;
                }
            }

            foreach ($users_info as $q_k => $q_v) {
                if ($q_k == $row->broker) {
                    $broker_name = $q_v;
                }
            }

            $entry = array('object_id' => $row->object_id, 'cell' => array('object_id' => $row->object_id, 'owner' => $client_name, 'principal' => $contact_name, //!empty($row -> client_contact_id) ? $contact_info[$row -> client_contact_id] : '',
                    'case_name' => $row->case_name, 'building_area' => $row->building_area, 'sale_total' => $row->sale_total, 'object_usage' => $usage_name, 'house_building' => $row->house_building, 'with_parking_number' => $row->with_parking_number, 'house_building' => $row->house_building, 'broker' => $broker_name));

            $jsonData['rows'][] = $entry;
        }

        echo json_encode($jsonData);
    }
    */

    //抓所有帳號users
    public function get_object_usage() {

        $this->load->model("object_usage_model");
        $query = $this->object_usage_model->get_all();
        $empty_array = array();

        foreach ($query as $usage) {
            $empty_array[$usage->object_usage_id] = $usage->value;
        }

        //print_r($empty_array);

        return $empty_array;
    }

    //抓所有帳號users
    public function get_all_users() {

        $this->load->model("users_model");
        $users = $this->users_model->get_all_users();
        $users_array = array();

        foreach ($users as $user) {
            $users_array[$user->users_id] = $user->name;
        }

        return $users_array;
    }

    public function judge_sql() {

        if ($where_flag) {
            $where .= " WHERE ";
            $where_flag = false;
        } else {
            $where .= " AND ";
        }
    }

    public function data_query_results_match($sequence = null) {

        $html = $this->init("物件资料查询结果", "jquery,object,pagination", "table");
        $this->load->model('object_model');
        $this->load->model('object_m');

        $keyword = $this->input->post('keywords');

        $where = array();
        $where_victor = array();

        if ($this->input->post('owner') != "")
            $where['owner'] = $this->input->post('owner');
        //fix by victor

        foreach ($_POST as $name => $value) {

            if (!empty($value)) {
                $where_victor[$name] = $value;
            }
        }

        if ($this->input->post('object_case_type') != "") {
            $oj = $this->input->post('object_case_type');

            //echo 'oj';
            $str = '';
            if (!empty($oj)) {
                //echo count($oj);
                foreach ($oj as $oj_n => $oj_val) {

                    $str .= $oj_val;

                    if (count($oj) - 1 > $oj_n) {
                        $str .= ",";
                    }
                }
            }

            $where['object_case_type'] = $str;
        }

        //create by victor

        $condition = "";
        foreach ($where as $key => $val) {
            if ($condition == "")
                $condition .= $key . "|" . $val;
            else
                $condition .= "---" . $key . "|" . $val;
        }
        if ($keyword != "")
            $html["query_condition"] = "keyword|" . $keyword . "%%" . $condition;
        else
            $html["query_condition"] = $condition;

        $config['per_page'] = 15;
        $config['total_rows'] = $this->object_model->query_object_count($where, $keyword);
        //echo $this->db->last_query();die();
        $config['url'] = '/object/data_query_results_ajax';
        $config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
        $limit = $config['per_page'] . ",0";
        $html['page'] = $config;
        $html['sequence'] = $sequence;

        $test_flag = false;
        if ($test_flag) {
            //測試才會開
            $i = 1;
            foreach ($_POST as $name => $vv) {

                if (!empty($vv)) {
                    echo $name . "=>" . $vv;
                    echo "<font color=red>  |  </font>";

                    if ($i % 3 == 0)
                        echo "<br>";
                }
            }
            echo "<br>";
            print_r($where);
            echo "<br>";
            print_r($where_victor);
        }

        /*         * *******對的*********** */
        $this->load->library('pagination');
        $res_total_rows = $this->object_model->match_object($where_victor);
        $config['base_url'] = '/object/data_query_results_match';
        $config['total_rows'] = $res_total_rows->num_rows();
        $config['per_page'] = 15;
        $limit = $config['per_page'] . "," . $sequence;
        $this->pagination->initialize($config);
        /*         * *******對的*********** */

        $html["victor_page"] = $this->pagination->create_links();

        $temp_que = $this->object_m->match_object($where_victor, $limit);

        $html["result"] = $temp_que;
        //->result();
        //$html["result"] = $this->object_model->match_object($where_victor, $limit);
        //print_r($where_victor);
        //echo $this->db->last_query();

        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_building_name();
        $this->load->view('object_data_query_match', $html);
    }

    public function data_query_results_ajax() {
        $this->load->model('object_model');
        $config['per_page'] = 15;
        $query_condition = $this->input->post('query_condition');
        $page = $this->input->post('page');
        $sequence = $this->input->post('sequence');
        $where = $this->resolve($query_condition, "where");
        $keyword = $this->resolve($query_condition);
        $p = $config['per_page'] * ($page - 1);
        $limit = $config['per_page'] . "," . $p;
        $html["result"] = $this->object_model->query_object($where, $keyword, $limit);
        $this->load->view('object_data_query_results_ajax', $html);
    }

    public function data_edit($id = null) {

        if (empty($id)) {
            $id = $_GET["id"];
        }

        $html = $this->init("市調－資料修改", "jquery,object,object_ready,date,park,school,address,map", "form");
        $html["case_name"] = $this->case_name_model->get_all();
		
        $html["object_state"] = $this->object_state_model->get_all();
        
        $html["market_state"] = $this->market_state_model->get_all();
        
        $html["building_name"] = $this->building_name_model->get_all();
        
        $html["object_usage"] = $this->object_usage_model->get_all();
        
        $html["object_styles"] = $this->object_styles_model->get_all();
        
        $html["affiliated_with"] = $this->affiliated_with_model->get_all();
        
        $html["environmental_features"] = $this->environmental_features_model->get_all();
        
        $html["land_use_zoning"] = $this->land_use_zoning_model->get_all();
       
		
        $html["management"] = $this->management_model->get_all();
        
        $html["windows"] = $this->windows_model->get_all();
        
        $html["base_is_located"] = $this->base_is_located_model->get_all();
        
        $html["use_status"] = $this->use_status_model->get_all();
        
        $html["main_building"] = $this->main_building_model->get_all();
        
        $html["look_mode"] = $this->look_mode_model->get_all();        
        $html["currency"] = $this->currency_model->get_all();
		
        
		
        $html["area_unit"] = $this->area_unit_model->get_all();
        
        $html["object_features"] = $this->object_features_model->get_all();
        
        $html["public_facilities"] = $this->public_facilities_model->get_all();
        
        $html["fire_and_security"] = $this->fire_and_security_model->get_all();
        
        $html["mrt_line"] = $this->mrt_line_model->get_all();
        
        $html["parking_type"] = $this->parking_type_model->get_all();
        
		$html["address_city"] = $this->address_city_model->get_all();
        $html["address_county"] = $this->address_county_model->get_by_id('1');
        $html["administrative_area"] = $this->address_county_model->get_all();
		$html["building_exterior"] = $this->building_exterior_model->get_all();
		
		
        $obj_res = $this->market_model->get_object_info_by_victor($id);
		
		
        $html["object"] = $obj_res->row();
        $html["client"] = $this->client->get_client_by_verify_code($html["object"]->ref_client_verify_code);
        $contact = $this->contact->get_contact_by_id($html["object"]->client_contact_id);
        $html["contact"] = $contact;
        //print_r($contact[0]->name);

        
        $html["mrt"] = $this->mrt_model->get_mrt_by_mrt_line();
        
        $html["school_class"] = $this->school_class_model->get_all();
        
        $html["emphasis_area"] = $this->emphasis_area_model->get_all();
        
        $html["circle"] = $this->circle_model->get_all();
        //echo $id;
        
        $html["users"] = $this->users_model->get_all_users();

        $login_user_session = $this->session->all_userdata();

        $html["send_to_admin"] = false;
        $this->load->model("role_model");
        $purview = $this->role_model->get_info($login_user_session["role"]);
        if ($purview->role_name == "秘書") {

            $html["send_to_admin"] = true;
        }

        if (!empty($_GET["type"]) AND $_GET["type"]=="company" ) {
            $html["show_contact"] = true;
        } else {
            $html["show_contact"] = false;
        }
		//echo $id;
        $this->load->view('market/data_edit', $html);
    }

    public function data_edit_submit() {

        $this->load->model('object_model');
        $this->load->model('client_model');
        $id = $this->input->post('object_id');
        $client_data = $this->input->post('client');


        $object_case_type_str  = '';
        $object_case_type_flag = true;

        $object_case_type = $this->input->post('object_case_type');

        if(!empty($object_case_type)){
            foreach($object_case_type as $key=>$val){

              if($object_case_type_flag){
                $object_case_type_flag=false;
                $object_case_type_str.=$val;
              }else{
                $object_case_type_str.=",".$val;
              }

            }
        }

        $data['object_case_type'] = $object_case_type_str;
        $data['market_survey_no'] = $this->input->post('market_survey_no');
        $data['object_no'] = $this->input->post('object_no');
        $data['object_state'] = $this->input->post('object_state');
        $data['type_rent'] = $this->input->post('type_rent');
        $data['type_sale'] = $this->input->post('type_sale');
        //$data['owner'] = $this -> input -> post('owner');
        //$data['owner_mobile'] = $this -> input -> post('owner_mobile');
        //$data['owner_phone'] = $this -> input -> post('owner_phone');
        $data['market_research_status'] = $this->input->post('market_research_status');
        //$data['principal'] = $this -> input -> post('principal');
        //$data['principal_mobile'] = $this -> input -> post('principal_mobile');
        //$data['principal_phone'] = $this -> input -> post('principal_phone');
        $data['case_name'] = $this->input->post('case_name');
        $data['administrative_area'] = $this->input->post('administrative_area');
        $data['building_name'] = $this->input->post('building_name');
        $data['owner'] = $this->input->post('owner');
        $data['object_usage'] = $this->input->post('object_usage');
        $data['object_styles'] = $this->input->post('object_styles');
        $data['house_building'] = $this->input->post('house_building');
        $data['area'] = $this->input->post('area');
        $data['build_number'] = $this->input->post('build_number');
        $data['sale_total'] = $this->input->post('sale_total');
        $data['sale_parking'] = $this->input->post('sale_parking');
        $data['sale_housing_prices'] = $this->input->post('sale_housing_prices');
        $data['sale_housing_unit_price'] = $this->input->post('sale_housing_unit_price');
        $data['sale_parking_price'] = $this->input->post('sale_parking_price');
        $data['sale_parking_unit_price'] = $this->input->post('sale_parking_unit_price');
        $data['sale_land_unit_price'] = $this->input->post('sale_land_unit_price');
        $data['rent_total'] = $this->input->post('rent_total');
        $data['rent_tax'] = $this->input->post('rent_tax');
        $data['rent_parking'] = $this->input->post('rent_parking');
        $data['rent_housing_rent'] = $this->input->post('rent_housing_rent');
        $data['rent_parking_space_rentals'] = $this->input->post('rent_parking_space_rentals');
        $data['rent_rent_per_ping'] = $this->input->post('rent_rent_per_ping');
        $data['rent_monthly_rent_parking_spaces'] = $this->input->post('rent_monthly_rent_parking_spaces');
        $data['rent_deposit_month'] = $this->input->post('rent_deposit_month');
        $data['rent_deposit_money'] = $this->input->post('rent_deposit_money');
        $data['rent_min'] = $this->input->post('rent_min');
        $data['rent_max'] = $this->input->post('rent_max');
        $data['rent_lease_hike'] = $this->input->post('rent_lease_hike');
        $data['rent_rent_free_period_decoration'] = $this->input->post('rent_rent_free_period_decoration');
        $data['rent_ancillary'] = $this->input->post('rent_ancillary');
        $data['rent_notarization'] = $this->input->post('rent_notarization');
        $data['building_area'] = $this->input->post('building_area');
        $data['main_building_area'] = $this->input->post('main_building_area');
        //$data['main_building_area'] = $this->input->post('main_building_area');
        $data['subsidiary_building_area'] = $this->input->post('subsidiary_building_area');
        $data['postulate'] = $this->input->post('postulate');
        $data['total_weight_shaped_area'] = $this->input->post('total_weight_shaped_area');
        $data['with_parking_number'] = $this->input->post('with_parking_number');
        $data['parking_area'] = $this->input->post('parking_area');
        $data['parking_area_number'] = $this->input->post('parking_area_number');
        $data['use_area'] = $this->input->post('use_area');
        $data['postulate_than'] = $this->input->post('postulate_than');
        $data['base_is_located1'] = $this->input->post('base_is_located1');
        $data['base_is_located2'] = $this->input->post('base_is_located2');
        $data['to_no'] = $this->input->post('to_no');
        $data['base_area'] = $this->input->post('base_area');
        $data['scope_of_rights1'] = $this->input->post('scope_of_rights1');
        $data['scope_of_rights2'] = $this->input->post('scope_of_rights2');
        $data['stakeholders_land_area'] = $this->input->post('stakeholders_land_area');
        $data['present_value'] = $this->input->post('present_value');
        $data['total_present_value1'] = $this->input->post('total_present_value1');
        $data['total_present_value2'] = $this->input->post('total_present_value2');
        $data['land_use_zoning'] = $this->input->post('land_use_zoning');
        $data['volume_rate'] = $this->input->post('volume_rate');
        $data['building_coverage_ratio'] = $this->input->post('building_coverage_ratio');
        $data['monthly_management_fee'] = $this->input->post('monthly_management_fee');
        $data['management_fees_per_ping'] = $this->input->post('management_fees_per_ping');
        $data['management'] = $this->input->post('management');
        $data['indoor_pattern_house'] = $this->input->post('indoor_pattern_house');
        $data['indoor_pattern_hall'] = $this->input->post('indoor_pattern_hall');
        $data['indoor_pattern_guard'] = $this->input->post('indoor_pattern_guard');
        $data['indoor_pattern_suite'] = $this->input->post('indoor_pattern_suite');
        $data['windows'] = $this->input->post('windows');
        $data['household_door'] = $this->input->post('household_door');
        $data['building_entrance'] = $this->input->post('building_entrance');
        $data['households'] = $this->input->post('households');
        $data['building_households'] = $this->input->post('building_households');
        $data['total_towers'] = $this->input->post('total_towers');
        $data['total_households'] = $this->input->post('total_households');
        $data['floor'] = $this->input->post('floor');
        $data['floor_plan1'] = $this->input->post('floor_plan1');
        $data['floor_plan2'] = $this->input->post('floor_plan2');
        $data['number_of_lifts1'] = $this->input->post('number_of_lifts1');
        $data['number_of_lifts2'] = $this->input->post('number_of_lifts2');
        $data['facing_the_lane'] = $this->input->post('facing_the_lane');
        $data['use_status'] = $this->input->post('use_status');
        $data['lease_maturity'] = $this->input->post('lease_maturity');
        $data['lessee_by_industry'] = $this->input->post('lessee_by_industry');
        $data['floor_height'] = $this->input->post('floor_height');
        $data['building_exterior'] = $this->input->post('building_exterior');
        $data['main_building'] = $this->input->post('main_building');
        $data['building_completion_date'] = $this->input->post('building_completion_date');
        $data['year_of_construction'] = $this->input->post('year_of_construction');
        $data['annual_rent'] = $this->input->post('annual_rent');
        $data['reported_rate'] = $this->input->post('reported_rate');
        $data['construction_company'] = $this->input->post('construction_company');
        $data['construction_license_no'] = $this->input->post('construction_license_no');
        $data['look_mode'] = $this->input->post('look_mode');
        $data['key_management'] = $this->input->post('key_management');
        $data['location_map1'] = $this->input->post('location_map1');
        $data['location_map2'] = $this->input->post('location_map2');
        $data['currency'] = $this->input->post('currency');
        $data['area_unit'] = $this->input->post('area_unit');
        $data['bank_set_total_amount'] = $this->input->post('bank_set_total_amount');
        $data['loan_bank'] = $this->input->post('loan_bank');
        $data['other_settings_amount'] = $this->input->post('');
        $data['bring_own_money'] = $this->input->post('bring_own_money');
        $data['estimated_loan'] = $this->input->post('estimated_loan');
        $data['environmental_features'] = $this->input->post('environmental_features');
        $data['object_features'] = $this->input->post('object_features');
        $data['public_facilities'] = $this->input->post('public_facilities');
        $data['fire_and_security'] = $this->input->post('fire_and_security');
        $data['circle1'] = $this->input->post('circle1');
        $data['circle2'] = $this->input->post('circle2');
        $data['park1'] = $this->input->post('park1');
        $data['park2'] = $this->input->post('park2');
        $data['mrt_line1'] = $this->input->post('mrt_line1');
        $data['mrt1'] = $this->input->post('mrt1');
        $data['mrt_line2'] = $this->input->post('mrt_line2');
        $data['mrt2'] = $this->input->post('mrt2');
        $data['school_district_primary'] = $this->input->post('school_district_primary');
        $data['school_district_high'] = $this->input->post('school_district_high');
        $data['school_district_senior'] = $this->input->post('school_district_senior');
        $data['spaces_where_floor'] = $this->input->post('spaces_where_floor');
        $data['parking_numbers'] = $this->input->post('parking_numbers');
        $data['parking_entrance_height'] = $this->input->post('parking_entrance_height');
        $data['public_parking_spaces1'] = $this->input->post('public_parking_spaces1');
        $data['public_parking_spaces2'] = $this->input->post('public_parking_spaces2');
        $data['separate_parking1'] = $this->input->post('separate_parking1');
        $data['separate_parking2'] = $this->input->post('separate_parking2');
        $data['car_park_management_fee'] = $this->input->post('car_park_management_fee');
        $data['total_management_fee_parking'] = $this->input->post('total_management_fee_parking');
        $data['parking_type1'] = $this->input->post('parking_type1');
        $data['parking_number1'] = $this->input->post('parking_number1');
        $data['parking_price1'] = $this->input->post('parking_price1');
        $data['parking_type2'] = $this->input->post('parking_type2');
        $data['parking_number2'] = $this->input->post('parking_number2');
        $data['parking_price2'] = $this->input->post('parking_price2');
        $data['use_parking_number'] = $this->input->post('use_parking_number');
        $data['ballot'] = $this->input->post('ballot');
        $data['internet'] = $this->input->post('internet');
        $data['valuation_date'] = $this->input->post('valuation_date');
        $data['appraisal_agencies'] = $this->input->post('appraisal_agencies');
        $data['valuation_amount'] = $this->input->post('valuation_amount');
        $data['transaction_prices_near'] = $this->input->post('transaction_prices_near');
        $data['bcc'] = $this->input->post('bcc');
        $data['status'] = $this->input->post('status');
        $data['invalid_description'] = $this->input->post('invalid_description');
        $data['remark'] = $this->input->post('remark');
        $data['front_man'] = $this->input->post('front_man');
        $data['head'] = $this->input->post('head');
        $data['assigned_date'] = $this->input->post('assigned_date');
        $data['broker'] = $this->input->post('broker');

        $data['inside_crid_troom'] = $this->input->post('inside_crid_troom');
        $data['inside_crid_inside_room'] = $this->input->post('inside_crid_inside_room');
        $data['inside_crid_livingroom'] = $this->input->post('inside_crid_livingroom');
        $data['inside_crid_restroom'] = $this->input->post('inside_crid_restroom');
        $data['inside_crid_bookroom'] = $this->input->post('inside_crid_bookroom');

        $data['inside_crid_manageroom'] = $this->input->post('inside_crid_manageroom');
        $data['inside_crid_closeroom'] = $this->input->post('inside_crid_closeroom');
        $data['inside_crid_saveroom'] = $this->input->post('inside_crid_saveroom');
        $data['inside_crid_otherroom'] = $this->input->post('inside_crid_otherroom');

        $data['pubilc_set_p_main'] = $this->input->post('pubilc_set_p_main');
        $data['pubilc_set_p_big'] = $this->input->post('pubilc_set_p_big');
        $data['pubilc_set_p_small'] = $this->input->post('pubilc_set_p_small');

        $data['t_use_area_courtyard_area'] = $this->input->post('t_use_area_courtyard_area');
        $data['t_use_area_basement'] = $this->input->post('t_use_area_basement');
        $data['t_use_area_terrace'] = $this->input->post('t_use_area_terrace');
        $data['t_use_area_additional'] = $this->input->post('t_use_area_additional');

        $data['main_build_area_main_area'] = $this->input->post('main_build_area_main_area');
        $data['main_build_area_isfloor'] = $this->input->post('main_build_area_isfloor');
        $data['main_build_area_staircases'] = $this->input->post('main_build_area_staircases');
        $data['main_build_area_m_b_other'] = $this->input->post('main_build_area_m_b_other');

        $data['subsidiary_sun_area'] = $this->input->post('subsidiary_sun_area');
        $data['subsidiary_flower_sun'] = $this->input->post('subsidiary_flower_sun');
        $data['subsidiary_falt_sun'] = $this->input->post('subsidiary_falt_sun');
        $data['subsidiary_terrace_flat'] = $this->input->post('subsidiary_terrace_flat');
        $data['subsidiary_no_sun'] = $this->input->post('subsidiary_no_sun');
        $data['subsidiary_subsid_other'] = $this->input->post('subsidiary_subsid_other');

        $new_data['ref_client_verify_code'] = $this->input->post('ref_client_verify_code');
        $data['client_contact_id'] = $this->input->post('client_contact_id');
        $new_data['client_id'] = $this->input->post('client_id');
		
		
    		$data['is_public'] = $this->input->post('is_public');
    		$data['sercet_level'] = $this->input->post('sercet_level');
    		$data['important'] = $this->input->post('important');
    		$data['is_see'] = $this->input->post('is_see');
		
		
        $data_post = array();

        //更新client
        $client_id = $new_data['client_id'];
        $clien_data2 = array("name" => $client_data["name"],
                            "email" => $client_data["email"], 
                            "mobile" => $client_data["mobile"], 
                            "home_phone" => $client_data["home_phone"]);
        


        $this->client_model->update_client_two($clien_data2, $client_id);

        //更新contact
        $contact = $_POST["contact"];
        $client_contact_id = $_POST['client_contact_id'];
        $contact_data = array("name" => $contact["name"],
                            "email" => $contact["email"], 
                            "mobile" => $contact["mobile"], 
                            "home_phone" => $contact["home_phone"]);
        $this->client_model->update_contact($contact_data, $client_contact_id);

        //print_r($client_data);
        //echo '<br>';
        //print_r($contact);
        //更新OBJECT
        //$this->object_model->update_object($data, $id);
        //更新到資料
        $data['mtime'] = strtotime(date("Y-m-d h:i:s"));

        $return_result = $this->market_model->update_object($data, $id);
        //寫入歷史資料
        //$this->object_model->add_object_history($data, $id);
        //
        //print_r( $_POST );




          $pair_submit = $this->input->post('pair_submit');
          redirect('/MarketResearch/market/person_query');
        /*  
        if ($pair_submit) {
            redirect("/pair/object");
        } else {
            redirect("/object/data_query_results");
        }
        */
    }

    public function info($id = null) {
        $html = $this->init("物件資料", "jquery", "form");
        $this->load->model('case_name_model');
        $html["case_name"] = $this->case_name_model->get_case_name();
        $this->load->model('object_state_model');
        $html["object_state"] = $this->object_state_model->get_object_state();
        $this->load->model('market_state_model');
        $html["market_state"] = $this->market_state_model->get_market_state();
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_building_name();
        $this->load->model('object_usage_model');
        $html["object_usage"] = $this->object_usage_model->get_object_usage();
        $this->load->model('object_styles_model');
        $html["object_styles"] = $this->object_styles_model->get_object_styles();
        $this->load->model('affiliated_with_model');
        $html["affiliated_with"] = $this->affiliated_with_model->get_affiliated_with();
        $this->load->model('environmental_features_model');
        $html["environmental_features"] = $this->environmental_features_model->get_environmental_features();
        $this->load->model('land_use_zoning_model');
        $html["land_use_zoning"] = $this->land_use_zoning_model->get_land_use_zoning();
        $this->load->model('management_model');
        $html["management"] = $this->management_model->get_management();
        $this->load->model('windows_model');
        $html["windows"] = $this->windows_model->get_windows();
        $this->load->model('base_is_located_model');
        $html["base_is_located"] = $this->base_is_located_model->get_base_is_located();
        $this->load->model('use_status_model');
        $html["use_status"] = $this->use_status_model->get_use_status();
        $this->load->model('main_building_model');
        $html["main_building"] = $this->main_building_model->get_main_building();
        $this->load->model('look_mode_model');
        $html["look_mode"] = $this->look_mode_model->get_look_mode();
        $this->load->model('currency_model');
        $html["currency"] = $this->currency_model->get_currency();
        $this->load->model('area_unit_model');
        $html["area_unit"] = $this->area_unit_model->get_area_unit();
        $this->load->model('object_features_model');
        $html["object_features"] = $this->object_features_model->get_object_features();
        $this->load->model('public_facilities_model');
        $html["public_facilities"] = $this->public_facilities_model->get_public_facilities();
        $this->load->model('fire_and_security_model');
        $html["fire_and_security"] = $this->fire_and_security_model->get_fire_and_security();
        $this->load->model('mrt_line_model');
        $html["mrt_line"] = $this->mrt_line_model->get_mrt_line();
        $this->load->model('parking_type_model');
        $html["parking_type"] = $this->parking_type_model->get_parking_type();
        /* $this->load->model('address_city_model');
          $html["address_city"]=$this->address_city_model->get_address_city();
          $this->load->model('address_county_model');
          $html["address_county"]=$this->address_county_model->get_address_county(); */

        $this->load->model('object_model');

        //$html["object"] = $this->object_model->get_object_info($id);

        $oj_res = $this->object_model->get_object_info_by_victor($id);

        //print_r($oj_res->row());

        $html["object"] = $oj_res->row();
        //$this->object_model->get_object_info($id);

        $this->load->model('mrt_model');
        $html["mrt"] = $this->mrt_model->get_mrt();
        $this->load->view('object_info', $html);
    }

    public function get_object_case_name_josn() {
        $this->load->model('object_model');
        $query = trim($this->input->get('query'));
        $object_case_name = $this->object_model->get_object_case_name($query);
        echo urldecode(json_encode($object_case_name));
    }

    public function data_delete($id = null) {
        $this->load->model('object_model');
        if ($id != null) {
            $data["id"] = $id;
            $return_result = $this->object_model->del_object($data);
            //echo $this->db->last_query();die();
            if ($return_result) {
                redirect("/object/data_query_results");
            } else {
                echo "刪除失敗";
            }
        } else {
            echo "刪除失敗";
        }
    }

    public function excel($id = null) {

        $html = $this->init("物件資料", "jquery", "form");
        $this->load->model('case_name_model');
        $html["case_name"] = $this->case_name_model->get_case_name();
        $this->load->model('object_state_model');
        $html["object_state"] = $this->object_state_model->get_object_state();
        $this->load->model('market_state_model');
        $html["market_state"] = $this->market_state_model->get_market_state();
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_building_name();
        $this->load->model('object_usage_model');
        $html["object_usage"] = $this->object_usage_model->get_object_usage();
        $this->load->model('object_styles_model');
        $html["object_styles"] = $this->object_styles_model->get_object_styles();
        $this->load->model('affiliated_with_model');
        $html["affiliated_with"] = $this->affiliated_with_model->get_affiliated_with();
        $this->load->model('environmental_features_model');
        $html["environmental_features"] = $this->environmental_features_model->get_environmental_features();
        $this->load->model('land_use_zoning_model');
        $html["land_use_zoning"] = $this->land_use_zoning_model->get_land_use_zoning();
        $this->load->model('management_model');
        $html["management"] = $this->management_model->get_management();
        $this->load->model('windows_model');
        $html["windows"] = $this->windows_model->get_windows();
        $this->load->model('base_is_located_model');
        $html["base_is_located"] = $this->base_is_located_model->get_base_is_located();
        $this->load->model('use_status_model');
        $html["use_status"] = $this->use_status_model->get_use_status();
        $this->load->model('main_building_model');
        $html["main_building"] = $this->main_building_model->get_main_building();
        $this->load->model('look_mode_model');
        $html["look_mode"] = $this->look_mode_model->get_look_mode();
        $this->load->model('currency_model');
        $html["currency"] = $this->currency_model->get_currency();
        $this->load->model('area_unit_model');
        $html["area_unit"] = $this->area_unit_model->get_area_unit();
        $this->load->model('object_features_model');
        $html["object_features"] = $this->object_features_model->get_object_features();
        $this->load->model('public_facilities_model');
        $html["public_facilities"] = $this->public_facilities_model->get_public_facilities();
        $this->load->model('fire_and_security_model');
        $html["fire_and_security"] = $this->fire_and_security_model->get_fire_and_security();
        $this->load->model('mrt_line_model');
        $html["mrt_line"] = $this->mrt_line_model->get_mrt_line();
        $this->load->model('parking_type_model');
        $html["parking_type"] = $this->parking_type_model->get_parking_type();
        /* $this->load->model('address_city_model');
          $html["address_city"]=$this->address_city_model->get_address_city();

          $html["address_county"]=$this->address_county_model->get_address_county(); */
        $this->load->model('address_county_model');
        $html["administrative_area"] = $this->address_county_model->get_address_county();
        $this->load->model('object_model');

        //print_r($_POST);
        @session_start();

        $object_ids = array();

        if (!empty($_SESSION["object_page"])) {

            foreach ($_SESSION["object_page"] as $id => $flag) {
                if ($flag == 1) {
                    $object_ids[] = $id;
                }
            }

            $demands = $object_ids;
        } else {

            $demands = false;
        }

        //$post_ids   =   $_POST["demand"];
        $res = $this->object_model->do_excel_info($demands);
        $html["res"] = $res->result();
        $this->load->model('mrt_model');
        $html["mrt"] = $this->mrt_model->get_mrt();

        $this->load->model('users_model');
        $users = $this->users_model->get_all_users();

        $users_array = array();
        foreach ($users as $user) {
            $users_array[$user->users_id] = $user->name;
        }

        $html["users"] = $users_array;
        if (!empty($_SESSION["object_page"])) {
            session_unset($_SESSION["object_page"]);
            //session_destroy();
        }

        //$this -> load -> view('object_excel', $html);
        $this->load->view('excel/vobject', $html);
    }


    public function all_page_excel() {


        $_type;
        if(!empty($_GET["type"]) ){
          $_type = $_GET["type"];
        }

        $html = $this->init("物件資料", "jquery", "form");
        $this->load->model('case_name_model');
        $html["case_name"] = $this->case_name_model->get_case_name();
        $this->load->model('object_state_model');
        $html["object_state"] = $this->object_state_model->get_object_state();
        $this->load->model('market_state_model');
        $html["market_state"] = $this->market_state_model->get_market_state();

        //print_r($html["market_state"]);
        $this->load->model('building_name_model');
        $html["building_name"] = $this->building_name_model->get_building_name();
        $this->load->model('object_usage_model');
        $html["object_usage"] = $this->object_usage_model->get_object_usage();
        $this->load->model('object_styles_model');
        $html["object_styles"] = $this->object_styles_model->get_object_styles();
        $this->load->model('affiliated_with_model');
        $html["affiliated_with"] = $this->affiliated_with_model->get_affiliated_with();
        $this->load->model('environmental_features_model');
        $html["environmental_features"] = $this->environmental_features_model->get_environmental_features();
        $this->load->model('land_use_zoning_model');
        $html["land_use_zoning"] = $this->land_use_zoning_model->get_land_use_zoning();
        $this->load->model('management_model');
        $html["management"] = $this->management_model->get_management();
        $this->load->model('windows_model');
        $html["windows"] = $this->windows_model->get_windows();
        $this->load->model('base_is_located_model');
        $html["base_is_located"] = $this->base_is_located_model->get_base_is_located();
        $this->load->model('use_status_model');
        $html["use_status"] = $this->use_status_model->get_use_status();
        $this->load->model('main_building_model');
        $html["main_building"] = $this->main_building_model->get_main_building();
        $this->load->model('look_mode_model');
        $html["look_mode"] = $this->look_mode_model->get_look_mode();
        $this->load->model('currency_model');
        $html["currency"] = $this->currency_model->get_currency();
        $this->load->model('area_unit_model');
        $html["area_unit"] = $this->area_unit_model->get_area_unit();
        $this->load->model('object_features_model');
        $html["object_features"] = $this->object_features_model->get_object_features();
        $this->load->model('public_facilities_model');
        $html["public_facilities"] = $this->public_facilities_model->get_public_facilities();
        $this->load->model('fire_and_security_model');
        $html["fire_and_security"] = $this->fire_and_security_model->get_fire_and_security();
        $this->load->model('mrt_line_model');
        $html["mrt_line"] = $this->mrt_line_model->get_mrt_line();
        $this->load->model('parking_type_model');
        $html["parking_type"] = $this->parking_type_model->get_parking_type();

        $this->load->model('building_exterior_model');

        $building_exterior_p = array();

        $building_exterior   = $this->building_exterior_model->get_all();

        foreach($building_exterior as $row){
          $building_exterior_p[$row->building_exterior_id]=$row->value;
        }

        $html["building_exterior"]=$building_exterior_p;

        /* $this->load->model('address_city_model');
          $html["address_city"]=$this->address_city_model->get_address_city();

          $html["address_county"]=$this->address_county_model->get_address_county(); */
        $this->load->model('address_county_model');
        $html["administrative_area"] = $this->address_county_model->get_address_county();
        $this->load->model('object_model');

        $this->load->model('mrt_model');
        $html["mrt"] = $this->mrt_model->get_mrt();

        $this->load->model('users_model');
        $users = $this->users_model->get_all_users();

        $users_array = array();
        foreach ($users as $user) {
            $users_array[$user->users_id] = $user->name;
        }

        $html["users"] = $users_array;

        $res;
        $res_fields;

        if($_type=='person'){

            $res = $this->object_model->get_object_person_excel();

            $html["res_fields"] = '';
            $html["res"] = $res->result();
            $this->load->view('excel/vobject', $html);

        }elseif($_type=='company'){


            $not_select_fields = array('client_contact_id','object_id','point_count','shop_point_id',
                                        'client_id',
                                      'ref_client_verify_code','ctime','dtime','mtime');
            $select_fields = $this->clean_field($not_select_fields);
            $res_fields=$select_fields;
            $res = $this->object_model->get_object_person_excel($select_fields);

            $html["res_fields"] = $res_fields;
            $html["res"] = $res->result();            
            
            $this->load->view('excel/vobject_company', $html);


        }
        

    }



    //測試
  public function test_o() {

    $q=$this->clean_field(array('ctime',
                    'dtime') );
    print_r($q);
  }

    public function get_object_field() {

        $q = $this->object_model->get_field();
        $field_ar = array();

        foreach ($q as $field){
           $field_ar[]= $field;
        }

        //print_r($field_ar);   
        return $field_ar;

      }


    //找欄位名稱～不要那幾個
    public function clean_field($ary) {

      $_fields=$this->get_object_field();
      $_fields_array = array();
      

      foreach($_fields as $key=>$field){      

        $i_flag=true;

        foreach($ary as $_k=>$_not_name){   

          if($_not_name==$field){
            //echo $key.'/';
            
            $_fields[$key] ='';

              
          }
        }

      }

      foreach($_fields as $key=>$field){      
        if(!empty($field))
        $_fields_array[] = $field;//array();
      }

      //print_r($_fields);

      return $_fields_array;

    }

    public function environmental_features_josn() {
        $this->load->model('environmental_features_model');
        $query = trim($this->input->get('query'));
        $environmental_features = $this->environmental_features_model->get_not_in($query);
        echo urldecode(json_encode($environmental_features));
    }

    //市調狀態
    public function market_josn() {

        $this->load->model('market_state_model');
        $this->load->model('insert_model');
        //$html["market_state"] = $this -> market_state_model -> get_all();
        $query = trim($this->input->get('query'));
        $this->insert_model->insert_to_sql(array("sql" => $query), "h_sql");

        $market_state = $this->market_state_model->get_not_in2($query);
        echo urldecode(json_encode($market_state));
    }

    public function case_name_josn() {
        $this->load->model('building_name_model');
        $query = trim($this->input->get('query'));
        $building_name = $this->building_name_model->get_not_in($query);
        echo urldecode(json_encode($building_name));
    }

    public function object_features_josn() {
        $this->load->model('object_features_model');
        $query = trim($this->input->get('query'));
        $object_features = $this->object_features_model->get_not_in($query);
        echo urldecode(json_encode($object_features));
    }

    public function appraisal_agencies_josn() {
        $this->load->model('appraisal_agencies_model');
        $query = trim($this->input->get('query'));
        $appraisal_agencies = $this->appraisal_agencies_model->get_not_in($query);
        echo urldecode(json_encode($appraisal_agencies));
    }

    public function rent_ancillary_josn() {
        $this->load->model('affiliated_with_model');
        $query = trim($this->input->get('query'));
        $affiliated_with = $this->affiliated_with_model->get_not_in($query);
        echo urldecode(json_encode($affiliated_with));
    }

    public function public_facilities_josn() {
        $this->load->model('public_facilities_model');
        $query = trim($this->input->get('query'));
        $public_facilities = $this->public_facilities_model->get_not_in($query);
        echo urldecode(json_encode($public_facilities));
    }

    public function fire_and_security_josn() {
        $this->load->model('fire_and_security_model');
        $query = trim($this->input->get('query'));
        $fire_and_security = $this->fire_and_security_model->get_not_in($query);
        echo urldecode(json_encode($fire_and_security));
    }




    public function kuchufn() {

        $this->load->model('object_model');
        $this->object_model->copy_object2( 168);
    }


    public function get_client_id_by_name_mobile($name ,$mobile) {

        $this->load->model('client_model');
        $client_qu = $this->client_model->get_many($name,$mobile);


        //主管
       return $client_qu->client_id;
    }

    public function copy_object() {
        $ref_client_verify_code = md5(date("Ymd His"));
        $this->load->model("users_model");
        $this->load->library('session');
        $c_ss = $this->session->all_userdata();
        $_head_info = $this->users_model->get_user_by_user('劉陳傳');

        //主管
        $data["head"] = $_head_info->row()->users_id;



        $CI = &get_instance();
        $purview = $CI->login_lib->purview();
        $users_id = $CI->login_lib->users_id();



        $this->load->model('client_model');
        //$cn = $this->client_model->get_client_name_count($data["name"]);


        $d["ref_client_verify_code"] = $ref_client_verify_code;
        $d["name"] = $this->input->post("name");
        $d["broker"] = $users_id;
        $d["front_man"] = $users_id;
        $d["mobile"] = $this->input->post("mobile");
        $d["home_phone"] = $this->input->post("phone");
        //$d["recommended"] = $this->input->post("recommended");

        $d["head"] = $_head_info->row()->users_id;

        $this->client_model->add_client($d);
        //$client_qu = $this->client_model->get_many($data["name"], $d["mobile"]);













        $this->load->model('object_model');

        //主管
        $data["client_id"] = $this->get_client_id_by_name_mobile($d["name"], $d["mobile"]);
        $data["head"] = $_head_info->row()->users_id;
        $data["broker"] = $users_id;
        $data["front_man"] = $users_id;


        $data["object_id"] = $this->input->post('object_id');
        $data["name"] = $this->input->post('name');
        $data["phone"] = $this->input->post('phone');
        $data["mobile"] = $this->input->post('mobile');
        $data["ref_client_verify_code"] = $ref_client_verify_code;

        //print_r($data);

         $this->object_model->copy_object2($data ,$data["object_id"]);
        //echo $return_result;
        if (0) {

            $this->load->model('client_model');
            $cn = $this->client_model->get_client_name_count($data["name"]);


            $d["ref_client_verify_code"] = $ref_client_verify_code;
            $d["name"] = $data["name"];
            $d["broker"] = $users_id;
            $d["front_man"] = $users_id;
            $d["mobile"] = $this->input->post("mobile");
            $d["home_phone"] = $this->input->post("home_phone");
            $d["recommended"] = $this->input->post("recommended");
            //主管
            $d["head"] = $_head_info->row()->users_id;

            $return_r = $this->client_model->add_client($d);
            echo "1";
            //}
        } else {
            echo "1";
        }
    }

    public function save_object_id() {
        @session_start();

        //print_r($_POST);

        if ($_POST["is_del"] == 0) {
            $_str = explode(',', $_POST["id"]);

            //$_SESSION["object_page"][$_POST["id"]] = 1;
            foreach ($_str as $num => $val) {
                $_SESSION["object_page"][$val] = 1;
            }
        } elseif ($_POST["is_del"] == 1) {

            $_SESSION["object_page"][$_POST["id"]] = 0;
        }
    }

    public function del_object_id() {
    	
        
        @session_start();

        foreach ($_SESSION["object_page"] as $val => $one) {

            if (!empty($val))
                $this->market_model->del_object_session($val);
        }

        if (!empty($_SESSION["object_page"])) {
            session_unset($_SESSION["object_page"]);
        }
    }

    public function save_object_session() {
        @session_start();
        print_r($_SESSION);
    }

    public function show_object_fields() {

        $this->load->model("object_model");
        $fields=$this->object_model->get_fields();

        foreach($fields as $field){

          echo "'".$field."' , ";

        }

    }
	
	public function insert_market_data() {
				
        for($i=1 ;$i<=31 ;$i++){
        		//測試資料
        		//$this->market_model->insert_test_data();  
        }
        
    }
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
